(function ($) {
    var style0 = {
        "display": "inline-block",
        "border-radius": "2px",
        "padding": "0",
        "margin": "0"
    };
    //li样式
    var style1 = {
        "border-radius": "2px",
        "background-color": "#F2F2F2",
        "margin": "1px",
        "padding": "1px",
        "color": "#69B3E5",
        "cursor": "default",
        "list-style": "none",
        "text-align": "center"
    };
    //ul 样式
    var style2 = {
        "position": "absolute",
        "border-radius": "2px",
        "border": "1px solid grey",
        "box-sizing": "border-box",
        "max-height": "80px",
        "overflow": "auto",
        "margin": "0",
        "padding": "0",
        "z-index": 2002,
        "display": "none"
    };
    // 蒙板样式
    var style3 = {
        "position": "fixed",
        "top": 0,
        "left": 0,
        "width": "100%",
        "height": "100%",
        "z-index": 2001,
        "display": "none"
    };
    //keyCode   字母和数字
    var keyCodeArr = (function () {
        var arr = [];
        for (var i = 45; i < 106; i++) {
            if ((i > 57 && i < 65) || (i > 90 && i < 96)) {
                continue;
            }
            arr.push(i);
        }
        return arr;
    })();
    // var keyCodeArr = [
    //     {code: 65, text: "a"},
    //     {code: 66, text: "b"},
    //     {code: 67, text: "c"},
    //     {code: 68, text: "d"},
    //     {code: 69, text: "e"},
    //     {code: 70, text: "f"},
    //     {code: 71, text: "g"},
    //     {code: 72, text: "h"},
    //     {code: 73, text: "i"},
    //     {code: 74, text: "j"},
    //     {code: 75, text: "k"},
    //     {code: 76, text: "l"},
    //     {code: 77, text: "m"},
    //     {code: 78, text: "n"},
    //     {code: 79, text: "o"},
    //     {code: 80, text: "p"},
    //     {code: 81, text: "q"},
    //     {code: 82, text: "r"},
    //     {code: 83, text: "s"},
    //     {code: 84, text: "t"},
    //     {code: 85, text: "u"},
    //     {code: 86, text: "v"},
    //     {code: 87, text: "w"},
    //     {code: 88, text: "x"},
    //     {code: 89, text: "y"},
    //     {code: 90, text: "z"},
    //     {code: 48, text: "0"},
    //     {code: 49, text: "1"},
    //     {code: 50, text: "2"},
    //     {code: 51, text: "3"},
    //     {code: 52, text: "4"},
    //     {code: 53, text: "5"},
    //     {code: 54, text: "6"},
    //     {code: 55, text: "7"},
    //     {code: 56, text: "8"},
    //     {code: 57, text: "9"},
    //     {code: 96, text: "0"},
    //     {code: 97, text: "1"},
    //     {code: 98, text: "2"},
    //     {code: 99, text: "3"},
    //     {code: 100, text: "4"},
    //     {code: 101, text: "5"},
    //     {code: 102, text: "6"},
    //     {code: 103, text: "7"},
    //     {code: 104, text: "8"},
    //     {code: 105, text: "9"}
    // ];
    //键盘keycode字符串
    // var keyCodePy = "";

    function inSel(data) {
        var thiz = $(this);
        thiz.each(function () {
            var el = $(this);
            el.wrap("<div/>");
            var p = el.parent();
            p.addClass("input_extend_sel");
            p.css(style0);
            addShadow(p);
            addItem(data, p);
            el.focus(ulShow);
            el.keyup({arr: data}, filter);
        });
    }


    /**
     * 根据输入筛选项
     * @param e  事件event;
     */
    function filter(e) {
        var dataArr = e.data.arr;
        var el = getTarget(e);
        var p = el.parent();
        var ul = p.find(".input_extend_ul");
        var elVal = el.val().trim();
        if (elVal.length > 0) {
            var data0 = arrFilter(dataArr, elVal);
            ul.empty();
            addLi(ul, data0);
        } else {
            var li = ul.find("li");
            if (li.length != dataArr.length) {
                ul.empty();
                addLi(ul, dataArr);
            }
        }
    }


    /**
     *
     * 选项筛选;
     * @param arr
     * @param elVal
     * @param keyCode
     * @return arr  筛选后项数组;
     */
    function arrFilter(arr, elVal) {
        var arr0 = arr.filter(function (item) {
            var sort = -1;
            var val = item.value;
            if (pinyinUtil && pinyinUtil.getFirstLetter) {
                var py = pinyinUtil.getFirstLetter(val, true);
            }
            if (/[a-zA-Z0-9]*/g.test() && py) {
                for (var i = 0; i < py.length; i++) {
                    var t0 = py[i].toLocaleLowerCase();
                    sort = t0.indexOf(elVal);
                    break;
                }
            } else {
                sort = val.indexOf(elVal);
            }
            if (sort > -1) {
                item.sort = sort;
                return true;
            }
            return false;
        });
        if(arr0.length>0){
            arr0.sort(function (a, b) { return a.sort-b.sort });
            return arr0;
        }
        return arr;
    }

    //显示选项
    function ulShow(e) {
        stopUp(e);
        var el = getTarget(e);
        var p = el.parent();
        p.find(".input_extend_shadow").show();
        p.find(".input_extend_ul").show();

    }

    //添加蒙板
    function addShadow(p) {
        var shadow = crEl("div");
        shadow.addClass("input_extend_shadow")
        shadow.css(style3);
        shadow.click({"el": p}, hideUl);
        p.append(shadow);
    }

    //隐藏选项和蒙板
    function hideUl(e) {
        stopUp(e);
        e = getEvent(e);
        var p = e.data.el;
        p.find(".input_extend_shadow").hide();
        p.find(".input_extend_ul").hide();
    }

    //添加选项ul
    function addItem(data, p) {
        var ul = crEl("ul");
        p.append(ul);
        ul.css(style2);
        var ow = p.find("input:text").outerWidth();
        ul.css("width", ow + "px");
        ul.addClass("input_extend_ul");
        addLi(ul, data);
        placeControl(p, ul);
    }

    //选项位置调整
    function placeControl(p, ul) {
        var inp = p.find("input:text");
        var windowHeight = $(window).height();        //获取浏览器窗口高度
        var offTop = inp.offset().top;        //获取文档高度
        var bh = windowHeight - offTop;
        var uh = ul.height();
        if (uh - bh > 0) {
            p.prepend(ul);
        }
    }

    //元素hover;
    function overEv(e) {
        var e = getTarget(e);
        var style1 = {"background-color": "#EAEAEA"};
        e.css(style1);
    }

    //元素hover;
    function outEv(e) {
        var e = getTarget(e);
        var style1 = {"background-color": "#F2F2F2"};
        e.css(style1);
    }

    //选项点击事件
    function liEvent(e) {
        stopUp(e);
        e = getEvent(e);
        var p = e.data.el;
        var el = getTarget(e);
        var key = el.data("key");
        p.find("input:text").val(key);
        p.find(".input_extend_shadow").hide();
        p.find(".input_extend_ul").hide();
    }

    //添加选项li;
    function addLi(ul, data) {
        if (data && Array.isArray(data) && data.length > 0) {
            var p = ul.parent();
            for (var i = 0; i < data.length; i++) {
                var t = data[i];
                var li = crEl("li");
                li.data("key", t.value);
                li.html(t.text);
                li.css(style1);
                li.mouseover(overEv);
                li.mouseout(outEv);
                li.click({"el": p}, liEvent);
                ul.append(li);
            }
        }
    }

    //修改选项内容
    function changeData(data) {
        this.each(function () {
            var thiz = $(this);
            var p = thiz.parent();
            var ul = p.find(".input_extend_ul");
            if (ul.length > 0) {
                ul.empty();
                addLi(ul, data);
            }
        })
    }

    /**
     *
     * @param tagName 标签名
     * @returns {*|HTMLElement}
     */
    function crEl(tagName) {
        var el = document.createElement(tagName);
        return $(el);
    }

    //获取事件event;
    function getEvent(e) {
        return e = e || window.event;
    }

//阻止冒泡
    function stopUp(e) {
        e = getEvent(e);
        if (e.stopPropagation) {
            e.stopPropagation();
        } else {
            e.cancelBubble = true;
        }
    }

//阻止默认
    function stopDefaut(e) {
        e = getEvent(e);
        if (e.preventDefault) {
            e.preventDefault();
        } else {
            e.returnValue = false;
        }
    }

//获得事件对象
    function getTarget(e) {
        e = getEvent(e);
        return $(e.target);
    }

    $.fn.extend({
        "inputSel": inSel,
        "changeData": changeData
    });
})(jQuery);